#include <jni.h>
#include <iostream>
//
// Created by fuweicong on 2023/6/27.
//
#include <android/log.h>
#include <sdptransform.hpp>

#define LOGE(...) __android_log_print(ANDROID_LOG_ERROR  , "ProjectName", __VA_ARGS__)



extern "C"
JNIEXPORT jstring JNICALL
Java_com_fm_mediasoup_sdpTransform_SdpTransform_parse(JNIEnv *env, jclass clazz, jstring sdp) {
    // TODO: implement parse()
   // 将 Java 字符串转换为 C++ 字符串
   const char *cString = env->GetStringUTFChars(sdp, nullptr);
   std::string sdpStr(cString);

   // 释放 Java 字符串所占用的内存
   env->ReleaseStringUTFChars(sdp, cString);

   json session = sdptransform::parse(sdpStr);
   std::string jsonString = session.dump();
   jstring outputString = env->NewStringUTF(jsonString.c_str());

   return outputString;
}
extern "C"
JNIEXPORT jstring JNICALL
Java_com_fm_mediasoup_sdpTransform_SdpTransform_write(JNIEnv *env, jclass clazz, jstring json_sdp) {
   // TODO: implement write()
   const char *cString = env->GetStringUTFChars(json_sdp, nullptr);
   std::string sdpJson(cString);

   // 释放 Java 字符串所占用的内存
   env->ReleaseStringUTFChars(json_sdp, cString);

   json session = json::parse(sdpJson);

   std::string sdpString = sdptransform::write(session);

   jstring outputString = env->NewStringUTF(sdpString.c_str());

   return outputString;
}